/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.uch.biblioteca.hibernateSpringDao;
import java.util.List;
import net.uch.biblioteca.mapping.BibLector;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.dao.DataAccessException;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Expression;


/**
 *
 * @author Vasquez
 */
public class HSBibLectorDAOImpl extends HibernateDaoSupport implements HSBibLectorDAO {
    
    @Override
    public List seleccionarLector ( String descripcion, String sLecTip ) throws Exception {
        Criteria c = this.getSession().createCriteria( BibLector.class ).
                add( Restrictions.eq( "lecActivo", "1" ) );

        if ( descripcion.trim().length() != 0 ) {
            c.add(Restrictions.disjunction()
                    .add( Restrictions.like( "lecNombre", "%" + descripcion + "%" ))
                    .add( Restrictions.like( "lecPaterno", "%" + descripcion + "%" ))
                    .add( Restrictions.like( "lecMaterno", "%" + descripcion + "%" ))
                    );
                    
        }
        if(!"0".equals(sLecTip)){
            c.add(Restrictions.eq("lecTip", sLecTip));
        }
        return c.list();
    }

    @Override
    public void guardarLector ( BibLector lector ) throws Exception {
        this.getHibernateTemplate().save( lector );
    }

    @Override
    public void actualizarLector ( BibLector lector ) throws Exception {
        this.getHibernateTemplate().update( lector );
    }
    
    @Override
    public void eliminarLector( int iLecid ) throws Exception{
        String sHqlUpdate;

        sHqlUpdate = "update BibLector set lecActivo = :activo where lecId = :id";
        this.getSession().createQuery( sHqlUpdate ).setString( "activo", "0" ).
                setInteger( "id", iLecid ).
                executeUpdate();
    }
    
    @Override
    public BibLector buscarLector( int iLecId ) throws Exception{
        return (BibLector) this.getSession().createCriteria( BibLector.class ).
                add( Restrictions.eq( "lecId", iLecId ) ).uniqueResult();
    }
    
    @Override
    public BibLector validarLector ( String strUserName, String password )
            throws DataAccessException, java.sql.SQLException {

        BibLector lec = null;

        DetachedCriteria criteria = DetachedCriteria.forClass( BibLector.class );
        criteria.add( Expression.eq( "lecCodigo", strUserName ) );
        criteria.add( Expression.eq( "lecActivo", "1" ) );
        List<BibLector> lector = getHibernateTemplate().findByCriteria( criteria );

        if ( lector != null ) {
            for ( int i = 0; i < lector.size(); i++ ) {
                if ( password.equals( (lector.get( i )).getLecPassword() ) ) {
                    lec = lector.get( i );
                }
            }
        }
        return lec;
    }
    
    public BibLector lectorxCodigo ( String sLec_codigo ) throws Exception{
        
        return (BibLector) this.getSession().createCriteria( BibLector.class ).
                add( Restrictions.eq( "lecCodigo", sLec_codigo ) )
               .add( Restrictions.eq( "lecActivo", "1" ) )
                .uniqueResult();
    }
}
